import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import { getMvUrl } from '@/api/api.js'

export default new Vuex.Store({
  state: {
    isShow: false,
    // 二维码key
    unikey: '',
    cookie: '',
    // 控制视图
    changeItem1: 0,
    token: '',
    // 登录状态
    isLogin: false,
    userDetail: {},
    swiperImg: [],
    // 正在播放的URL
    songURl: [],
    mv: []
  },
  mutations: {
    // 登录界面的显示
    editIsShow(state) {
      state.isShow = !state.isShow
    },
    // 二维码key
    editUnikey(state, unikey) {
      state.unikey = unikey
    },
    // cookie
    setCookie(state, cookie) {
      state.cookie = cookie,
        window.sessionStorage.setItem('cookie', cookie)
    },
    // 切换的视图
    editChangeItem1(state, index) {
      state.changeItem1 = index
    },
    // 保存token
    saveToken(state, token) {
      state.token = token,
        window.sessionStorage.setItem('token', token)
    },
    // 保存登录状态
    saveIsLogin(state, payload) {
      state.isLogin = payload
    },
    // 保存用户信息
    saveUserDetail(state, userDetail) {
      state.userDetail = userDetail
      window.sessionStorage.setItem('userDetail', JSON.stringify(userDetail))
    },
    savaSwiperImg(state, swiperImg) {
      state.swiperImg = swiperImg
    },
    savaSongURL(state, songList) {
      state.songURl = songList
    },
    // mv
    savaMvUrl(state, mv) {
      state.mv = mv
      console.log(mv.url);
    }
  },
  actions: {
    // 获取mv
    async savaMvUrl({ commit }, payload) {
      const res = await getMvUrl(payload.id, payload.r)
      commit('savaMvUrl', res.data)
    },
    saveToken({ commit }, payload) {
      commit('saveToken', payload)
    },
    saveIsLogin({ commit }, payload) {
      commit('saveIsLogin', payload)
    },
    saveUserDetail({ commit }, payload) {
      commit('saveUserDetail', payload)
    },
    savaSwiperImg({ commit }, payload) {
      commit('savaSwiperImg', payload)
    },
    savaSongURL({ commit }, payload) {
      commit('savaSongURL', payload)
    }
  },
  modules: {
  }
})
